package com.example.woke;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.woke.fragment.BankCardListFragment;
import com.zhongcai.online.R;

/**
 *
 */
public class BankCardListActivity extends AppCompatActivity implements OnPageChangeListener, TabLayout.OnTabSelectedListener {

    private ViewPager mViewPager;
    private BankCardNoPagerAdapter mPromoteAdapter;
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private int mSort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_tablayout_viewpager);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.addOnTabSelectedListener(this);
        initTabLayoutAndViewPager();

    }

    private void initTabLayoutAndViewPager() {

        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView("全部")));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView("充值卡")));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabItemCustonView("到账卡")));
        initToolbarBg();
        mPromoteAdapter = new BankCardNoPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPromoteAdapter);
        mViewPager.setOnPageChangeListener(this);
    }


	/*----------------------------------------------------------------------*/

    private class BankCardNoPagerAdapter extends FragmentPagerAdapter {

        public BankCardNoPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int arg0) {
            BankCardListFragment fragment = new BankCardListFragment();
            Bundle args = new Bundle();
            args.putInt("type", arg0);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }

    }


    private int mPreviousScrollState;
    private int mScrollState;

    @Override
    public void onPageScrollStateChanged(int state) {
        mPreviousScrollState = mScrollState;
        mScrollState = state;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mTabLayout != null) {
            final boolean updateText = (mScrollState == ViewPager.SCROLL_STATE_DRAGGING)
                    || (mScrollState == ViewPager.SCROLL_STATE_SETTLING
                    && mPreviousScrollState == ViewPager.SCROLL_STATE_DRAGGING
            );
            mTabLayout.setScrollPosition(position, positionOffset, updateText);
        }

    }

    @Override
    public void onPageSelected(int position) {
        try {
            mTabLayout.getTabAt(position).select();
        } catch (Exception e) {
        }
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition(), false);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }


    private View getTabItemCustonView(String title) {
        View rootView = LayoutInflater.from(this).inflate(R.layout.tablayout_tv, null);
        TextView tv = (TextView) rootView.findViewById(R.id.tab_tv);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        tv.setText(title);
        return rootView;
    }

    private void initToolbarBg() {
        if (mTabLayout != null) {
            int count = mTabLayout.getTabCount();
            try {
                if (count > 1) {
                    for (int i = 0; i < count; i++) {
                        View tabView = (View) mTabLayout.getTabAt(i).getCustomView().getParent();
                        if (i == 0) {
                            //第一个
                            tabView.setBackgroundResource(R.drawable.zjs_tab_left_selector);
                        } else if (i == count - 1) {
                            //最后一个
                            tabView.setBackgroundResource(R.drawable.zjs_tab_right_selector);
                        } else {
                            //中间选项
                            tabView.setBackgroundResource(R.drawable.zjs_tab_middle_selector);
                        }
                    }
                } else if (count == 1) {
                    View tabView = (View) mTabLayout.getTabAt(0).getCustomView().getParent();
                    tabView.setBackgroundResource(R.drawable.zjs_tab_whole_selector);
                }
            } catch (Exception e) {
            }

        }
    }

}
